/*
 * Copyright (C) 2006 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.catonine.swing.rule;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.charset.Charset;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import nu.zoom.catonine.stylerule.StyleRules;
import nu.zoom.swing.layout.VerticalPanel;
import nu.zoom.ui.Resources;
import nu.zoom.ui.Resources.ResourceNotFoundException;

/**
 * A panel to edit the style rules collection meta-data. Such as name of the
 * collection, the block divider reg.exp. and so on.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 * 
 */
public class StyleRulesDataEditPanel extends JPanel implements DocumentListener {

	private static final long serialVersionUID = 1L;
	private final StyleRules styleRules;
	private final JTextField styleRulesName;

	public StyleRulesDataEditPanel(final Resources messages, final StyleRules styleRules)
			throws ResourceNotFoundException {
		this.styleRules = styleRules;

		styleRulesName = new JTextField(styleRules.getName());
		styleRulesName.getDocument().addDocumentListener(this);

		JLabel lbLabelLabel = new JLabel(messages.getMessage("nu.zoom.catonine.lblabel"));
		// Start or end combobox
		final String slbMessage = messages.getMessage("nu.zoom.catonine.slb");
		final String elbMessage = messages.getMessage("nu.zoom.catonine.elb");
		String[] lbModel = { elbMessage, slbMessage };
		final JComboBox lbStartOrEnd = new JComboBox(lbModel);
		lbStartOrEnd.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String lbMessage = (String) lbStartOrEnd.getSelectedItem();
				styleRules.setLogBlockPatternBeginsBlock(lbMessage.equals(slbMessage));
			}
		});
		lbStartOrEnd.setSelectedItem((styleRules.isLogBlockPatternBeginsBlock() ? slbMessage : elbMessage));
		LogBlockPatternField logBlockPatternField = new LogBlockPatternField(styleRules, lbLabelLabel);
		logBlockPatternField.setText(styleRules.getLogBlockPattern().toString());

		JPanel logBlockBoundaryPanel = new JPanel(new BorderLayout());
		logBlockBoundaryPanel.add(lbStartOrEnd, BorderLayout.WEST);
		logBlockBoundaryPanel.add(logBlockPatternField, BorderLayout.CENTER);

		JComboBox charsetBox = new JComboBox(new CharsetListModel(styleRules));
		charsetBox.setEditable(false);
		charsetBox.setSelectedItem(Charset.defaultCharset());

		VerticalPanel mainPanel = new VerticalPanel();
		mainPanel.addRow(messages.getMessage("nu.zoom.catonine.configurations.name"), styleRulesName);
		mainPanel.addRow(lbLabelLabel, logBlockBoundaryPanel);
		mainPanel.addRow(messages.getMessage("nu.zoom.catonine.encoding"), charsetBox);

		setLayout(new BorderLayout());
		add(mainPanel, BorderLayout.CENTER);
		// Building the GUI caused components to think they are changed (notably
		// LogBlockPatternField). It would be tricky to introduce an alternative
		// code path for mutating field documents and bypass validation.
		// So we simply reset the changed status after we are done.
		styleRules.resetChangeStatus();
	}

	@Override
	public void changedUpdate(DocumentEvent e) {
		styleRulesNameChanged();
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		styleRulesNameChanged();
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		styleRulesNameChanged();
	}

	private void styleRulesNameChanged() {
		styleRules.setName(styleRulesName.getText());
	}
}
